﻿using CanDoo.Admin.Core.Contracts;
using CanDoo.Admin.Core.Identity;
using CanDoo.Core.Context;
using CanDoo.Core.Data;
using CanDoo.Core.Logging;
using CanDoo.Core.Security;
using CanDoo.Data;
using CanDoo.Extensions;
using CanDoo.Filter;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Oil.Core.DTOs;
using CanDoo.Oil.WebApi.Api.Results;
using Microsoft.Owin.Security;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CanDoo.Oil.WebHost.Areas.Api.Controllers
{
    [Description("卡API")]
    [RoutePrefix("api/customer")]
    public class CustomerController : ApiController
    {
        #region owin

        public IAuthenticationManager AuthenticationManager { get; private set; }//认证管理
        public SignInManager SignInManager { get; private set; }//登陆管理

        #endregion

        #region Manager

        public IPermissionManager PermissionManager { get; private set; }
        public IOperateLoger OperateLoger { get; set; }
        public IOperator Operator { get; set; }
        public IDB DB { get; set; }

        /// <summary>
        /// 售油机序列号
        /// </summary>
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }//客户管理
        public IUserContract UserContract { get; set; }//用户管理

        /// <summary>
        /// Api操作日志
        /// </summary>
        public IAPIOperateLogContract APIOperateLogContract { get; set; }
        public IStoreActivityContract StoreActivityContract { get; set; }

        public IProductContract ProductContract { get; set; }

        /// <summary>
        /// 取油吗
        /// </summary>
        public IGetOilCodeContract GetOilCodeContract { get; set; }

        public IOilSerialNumContract OilSerialNumContract { get; set; }

        public IMediaADContract MediaADContract { get; set; }

        #endregion

        #region 属性

        private string _PageCode;
        public string PageCode
        {
            get
            {
                if (string.IsNullOrEmpty(_PageCode))
                {
                    _PageCode = this.GetType().Name;
                }
                return _PageCode;
            }
        }

        private string _pageTitle;
        public string PageTitle
        {
            get
            {
                if (string.IsNullOrEmpty(_pageTitle))
                {
                    _pageTitle = this.GetType().ToDescription();
                }
                return _pageTitle;
            }
        }

        #endregion


        public IOrderApiContract OrderApiContract { get; set; }

        #region ctor

        public CustomerController(IAuthenticationManager authenticationManager
            , SignInManager signInManager
            , IPermissionManager permissionManager
            )
        {
            AuthenticationManager = authenticationManager;
            SignInManager = signInManager;
            PermissionManager = permissionManager;

            PermissionManager.PageCode = PageCode;
            PermissionManager.PageTitle = PageTitle;
        }

        #endregion

        // GET: api/Customer
        public PageResult<CustomerApiInfo> Get(string CardNoStart,string CardNoEnd)
        {
            //1 储值 2 储油 3 售油机 管理卡4服务中心
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "储值");
            dic.Add(2, "储油");
            dic.Add(3, "售油机");
            var data = CustomerContract.GetPaged<CustomerApiInfo>(p => new CustomerApiInfo { Id = p.Id + "", PhysicalNo = p.PhysicalNo, CardNo = p.CardNo, Denomination = p.Denomination, LimitProductName = p.LimitProduct.Name, Name = p.Name, CardType = p.CardType, Balance = p.Balance, IsBurn = p.IsBurn }, filter: f => f.CardNo.CompareTo(CardNoStart) >= 0 && f.CardNo.CompareTo(CardNoEnd) <= 0, sortExpression: "Id", pagesize: 100, pageindex: 0);
            foreach (var item in data.Data)
            {
                item.CardTypeName = dic[item.CardType];
            }
            return data;
        }

        // GET: api/Customer/5
        public CustomerApiInfo Get(string id)
        {
            //1 储值 2 储油 3 售油机 管理卡4服务中心
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "储值");
            dic.Add(2, "储油");
            dic.Add(3, "售油机");

            CustomerApiInfo dto = null;

            var cardNo = id;
            var p = CustomerContract.FirstOrDefault(t=>t.CardNo == cardNo);
            DB.KeepReading(()=> {
                dto = new CustomerApiInfo { Id = p.Id + "", PhysicalNo = p.PhysicalNo, CardNo = p.CardNo, Name = p.Name, Denomination = p.Denomination, CardType = p.CardType, CardTypeName = dic[p.CardType], Balance = p.Balance, IsBurn = p.IsBurn };
                if (p.LimitProduct != null)
                    dto.LimitProductName = dto.LimitProductName;
            });
            return dto;
        }

        // POST: api/Customer
        public ApiResult Post(List<CustomerApiInfo> dtos)
        {
            var or = DB.Save(()=> {
                foreach (var dto in dtos)
                {
                    var id = dto.Id.ToLong();
                    var customer = CustomerContract.GetById(id);
                    customer.IsBurn = dto.IsBurn;
                    customer.PhysicalNo = dto.PhysicalNo;
                    customer.Denomination = dto.Denomination;

                    CustomerContract.UpdateEntity(customer);
                }
                return OperationResult.Success;
            });

            return new ApiResult(or);
        }

        // PUT: api/Customer/5
        public void Put(CustomerApiInfo dto)
        {

        }

        // DELETE: api/Customer/5
        public void Delete(int id)
        {
        }
    }
}
